package com.thinkfit.jy.adapter;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.thinkfit.jy.R;
import com.thinkfit.jy.bluetooth.Bluetooth;
import com.thinkfit.jy.utils.BtUtil;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

public class BondedBluetoothAdapter extends BaseQuickAdapter<BluetoothDevice, BaseViewHolder> {

    private Context mContext;
    private HashMap<String, Integer> mMap;


    public BondedBluetoothAdapter(Context context, int type) {
        super(R.layout.item_bonded_bluetooth);
        mContext = context;
        mMap = new HashMap<>();
    }

    public void changStatus(String address, int status) {
        mMap.put(address, status);
    }


    @SuppressLint("MissingPermission")
    @Override
    protected void convert(@NotNull BaseViewHolder helper, BluetoothDevice bean) {
        String name = bean.getName();
        int state;
        if (mMap.containsKey(bean.getAddress())) {
            state = mMap.get(bean.getAddress());
        } else {
            state = Bluetooth.getConnectStatus(bean.getAddress());
        }
        TextView tvName = helper.getView(R.id.tvName);
        tvName.setTextColor(mContext.getColor(R.color.white));
        String text = mContext.getString(R.string.app_not_connect);
        if (state == BluetoothAdapter.STATE_CONNECTED) {
            text = mContext.getString(R.string.app_connected);
            tvName.setTextColor(mContext.getColor(R.color.color_3D5AFE));
        } else if (state == BluetoothAdapter.STATE_CONNECTING) {
            text = mContext.getString(R.string.app_connecting);
        }


        //0 未知类型  1 传统类型  2 ble 类型  3  传统和ble双重类型
//        int type = bean.device.getType();

        int deviceType = BtUtil.getDeviceType(bean.getBluetoothClass());
        helper.setBackgroundResource(R.id.ioc_bluetooth, deviceType);

        helper.setText(R.id.tvName, TextUtils.isEmpty(name) || name.equals("NULL") ? bean.getAddress() : name)
                .setText(R.id.tvStatus, text)
                .setGone(R.id.line, getItemPosition(bean) == getItemCount() - 1);
    }




}
